<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/dev/plugins/bootstrap/css/bootstrap.css">
    <title>登录界面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-form {
            background-color: #fff;
            padding: 30px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }

        .login-form h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-control {
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        .input-group {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .btn {
            padding: 10px 15px;
            border-radius: 3px;
        }

        .btn-primary {
            width: 100%;
        }

        .btn-secondary {
            white-space: nowrap;
        }
        #email_span{
            /*隐藏*/
            display: none;
        }
    </style>
</head>

<body>
<script src="/dev/plugins/jquery/jquery.min.js"></script>
<script src="/dev/plugins/bootstrap/js/bootstrap.js"></script>
<div class="login-form">
    <h2>登录</h2>
    <form>
        <!-- 用户名输入框 -->
        <div class="form-group">
            <label for="email">用户名：</label>
            <input type="text" class="form-control" id="email" placeholder="请输入用户名">
            <span class="form-control-feedback glyphicon glyphicon-user"></span>
            <span class="text-danger" id="email_span">sorry,email is not exists</span>
        </div>

        <!-- 密码输入框 -->
        <!--        <div class="form-group">-->
        <!--            <label for="password">密码：</label>-->
        <!--            <input type="password" class="form-control" id="password" placeholder="请输入密码">-->
        <!--        </div>-->

        <!-- 验证码发送与输入框 -->
        <div class="form-group">
            <label for="code">验证码：</label>
            <div class="input-group">
                <input type="text" class="form-control" id="code" placeholder="请输入验证码">
                <button type="button" class="btn btn-secondary" id="sendCodeBtn" onclick="send()">发送验证码</button>
            </div>
        </div>

        <!-- 登录按钮 -->
        <button type="button" class="btn btn-primary" onclick="redirectToPage()">登录</button>
    </form>
</div>

<script>
    //1. 发送验证码按钮的单击事件
    function send(){
        //1. 获取邮箱输入框中的数据
        let email = document.getElementById("email").value;
        //2. 将这个email发送到后台(controller)
        $.get('/dev/user/sendCode?email='+email,function(res){
            //获取email错误的span标签对象
            let email_span = document.getElementById("email_span");
            if(res.code=='404'){
                //js让span显示
                email_span.style.display='block';
            }else{
                email_span.style.display='none';
                alert('邮箱发送成功!')
            }
        });
    }

    //2. 登录按钮
    function login(){
        //1.email和code分别获取一下
        let email = document.getElementById("email").value;
        let code = document.getElementById("code").value;
        //2.传参
        $.post('/dev/user/login',{"email":email,"code":code},function(res){
            if(res.code=='200'){//登录成功
                //获取server返回过来的token
                let token = res.data;
                //存储到本地存储中 - 判断是否属于登录状态
                localStorage.setItem("token",token);
                if (res.power == 0)
                    window.location='/dev/views/AdmIndex.html';
                else
                    window.location='/dev/views/UserIndex.html';
            }else if(res.code=='505'){
                alert(res.msg)
            }
        })
    }
    // 登录按钮点击直接跳转的函数
    function redirectToPage() {
        window.location.href = '/dev/views/UserIndex.html';
    }
</script>
</body>

</html>
